﻿namespace EFS.RC.CCAdmin.Skills
{
	/// Class Edit
	public partial class Edit : EFS.RC.CCAdmin.Common.Control
	{
		private EFS.Common.UI.Controls.ModalDialogHost host = null;

		#region Constructors
		/// Implicit
		public Edit()
		{
			InitializeComponent();
		}

		/// Explicit
		public Edit(
			EFS.Common.UI.Controls.ModalDialogHost host,
			System.Int32? skillID)
		{
			// check
			EFS.Common.Verify.Reference(host, "host");

			InitializeComponent();

			//set listener up
			UtilitiesService.Skills_GetDetailsCompleted += new System.EventHandler<WebProxy.Skills_GetDetailsCompletedEventArgs>(Skill_GetDetailsCompleted);

			//set new skill response listener
			UtilitiesService.Skill_CreateCompleted += new System.EventHandler<WebProxy.Skill_CreateCompletedEventArgs>(UtilitiesService_Skill_CreateCompleted);

			//set edit response listener
			UtilitiesService.Skill_UpdateCompleted += new System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(UtilitiesService_Skill_UpdateCompleted);

			this.host = host;
			this.host.OnButtonOKClick += OnParentOKClicked;

			// set ID
			this.SkillID = skillID;
		}
		#endregion

		#region Properties
		/// Property SkillID: Get, Set
		public System.Int32? SkillID { get; set;}

		/// Property IsValid: Get
		public System.Boolean? IsValid
		{
			get
			{
				return EFS.Common.Verify.String(SkillName);
			}
		}

		/// Property IsValid: Get
		public System.String SkillName
		{
			get
			{
				return EFS.Common.Functions.String.GetTrimmedString(textBoxSkillName.Text);
			}
		}
		#endregion

		#region LoadData
		/// Load data
		private void LoadData()
		{
			// if edit
			if (SkillID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while retrieving data");

				// call ws
				UtilitiesService.Skills_GetDetailsAsync(SkillID.Value);
			}
		}
		#endregion

		#region WS Callbacks
		/// Skill_GetDetailsCompleted
		private void Skill_GetDetailsCompleted(object sender, WebProxy.Skills_GetDetailsCompletedEventArgs e)
		{
			// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.Deployment.Entities.Skill skill =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.Deployment.Entities.Skill>(e.Result.ToString());

				//bind/fill form
				textBoxSkillName.Text = skill.Name;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve skill! " + ex.Message);
			}
		}

		/// Create skill completed
		private void UtilitiesService_Skill_CreateCompleted(object sender, WebProxy.Skill_CreateCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// close parent
				this.host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to create skill. " + ex.Message);
			}
		}

		/// Update skill completed
		private void UtilitiesService_Skill_UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// close parent
				this.host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to update skill. " + ex.Message);
			}
		}
		#endregion

		#region Events
		/// OnParentOKClicked
		private void OnParentOKClicked(object sender, System.Windows.RoutedEventArgs e, System.Boolean dialogResult)
		{
			// test if valid
			if (IsValid.HasValue && !IsValid.Value)
			{
				return;
			}

			// create
			if (!SkillID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while creating skill");

				//ws call
				UtilitiesService.Skill_CreateAsync(textBoxSkillName.Text);
			}
			else
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while updating skill");

				//ws call
				UtilitiesService.Skill_UpdateAsync(SkillID.Value, textBoxSkillName.Text);
			}
		}

		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//load data
				LoadData();
			}
		}
		#endregion
	}

}